package com.md.api.common.utils;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;

public class DistanceCalculator {

    private static final String API_KEY = "YOUR_API_KEY";  // 替换为你的API密钥
    private static final String URL = "";


    // origin  起点经纬度
    // destination 终点经纬度
    public static String calculateDistance(String origin, String destination) throws Exception {
        String requestUrl = URL + "?key=" + API_KEY + "&origins=" + origin + "&destinations=" + destination;
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpGet request = new HttpGet(requestUrl);
        HttpResponse response = httpClient.execute(request);

        String jsonResponse = EntityUtils.toString(response.getEntity());
        JSONObject jsonObject = new JSONObject(jsonResponse);
        return jsonObject.getJSONArray("results").getJSONObject(0).getString("distance");
    }
}